<?php
class Frame_Controller_User extends Frame_Controller_Base{

    public function init(){
        if(!$this->isLogin())
            return $this->redirect(MONK::_url('home/login'));
        $this->assign('my',$this->user);
        if(!$this->teamFilter())
            return $this->redirect(MONK::_url('team/opt'));
        $this->assign('team_id',$this->team_id);
        $this->assign('my_teams',$this->user_teams);
        $this->assign('team_creater_id',$this->team_creater_id);
        parent::init();
    }

    //团队人员列表
    public function actionIndex(){
        $model_user = MONK::getSingleton('Frame_Model_User');
        $users = $model_user->get_users_by_team_id($this->team_id);
        $this->assign('users',$users);
        return $this->render();
    }

    //单个用户信息
    public function actionOne(){
        $manage = false;
        $this->_setType(array('user_id'=>PARAM_STRING));
        $user_id = $this->_get('user_id');
        $model_user = MONK::getSingleton('Frame_Model_User');
        //用户是否属于当前团队
        if(!$model_user->is_team_user($this->team_id, $user_id))
            return $this->redirect(MONK::_url('home/404'));
        $model_app  = MONK::getSingleton('Frame_Model_App');
        $user = $model_user->get_user_by_id($user_id);
        $user['user_id'] = $user_id;
        $apps = $model_app->get_apps_by_user_id($user_id);
        if($this->isAdmin() && !$this->isMy($user_id))
            $manage = true;
        $this->assign('user',$user);
        $this->assign('apps',$apps);
        $this->assign('manage',$manage);
        return $this->render();
    }

    //邀请新成员
    public function actionInvite(){
        //判断是否有权限进行邀请
        if(!$this->isAdmin()){
            return $this->sendStatus(false,'您没有权限邀请新成员',10404);
        }
        //获取当前团队的应用列表
        $model_app = MONK::getSingleton('Frame_Model_App');
        $apps = $model_app->get_apps_by_team_id($this->team_id);
        $this->assign('apps',$apps);
        return $this->render();
    }

    //设置其他用户
    public function actionUsersetting(){
        $this->_setType(array('user_id'=>PARAM_STRING));
        $user_id = $this->_get('user_id');
        //设置的用户为自己
        if($this->isMy($user_id))
            return $this->redirect(MONK::_url('*/mysetting'));
        //不是管理员
        if(!$this->isAdmin())
            return $this->redirect(MONK::_url('home/403'));

        $model_user = MONK::getSingleton('Frame_Model_User');
        //用户是否属于当前团队
        if(!$model_user->is_team_user($this->team_id, $user_id))
            return $this->redirect(MONK::_url('home/404'));
        $user = $model_user->get_user_by_id($user_id);
        $user['user_id'] = $user_id;
        $this->assign('user',$user);
        return $this->render();
    }
    
    //从团队中删除用户
    public function actionDelete_AJAX_POST(){
        if(!$this->isAdmin())
            return $this->redirect(MONK::_url('home/403'));
        $this->_setType(array('user_id'=>PARAM_STRING));
        $user_id = $this->_get('user_id');
        $model_user = MONK::getSingleton('Frame_Model_User');
        if($model_user->delete_from_team_by_id($this->team_id, $user_id)){
            return $this->sendStatus(true, '删除成功');
        }else{
            return $this->sendStatus(false, '删除失败', '', 10300);
        }
    }

    //用户设置
    public function actionMysetting(){
        return $this->render();
    }
    public function actionMysetting_POST(){
        $this->_setType(array('email'=>PARAM_EMAIL,'nickname'=>PARAM_STRING,'maxim'=>PARAM_STRING/*,'mobile'=>PARAM_MOBILE*/,'old_password'=>PARAM_STRING,'new_password'=>PARAM_STRING),'post');
        $email = $this->_post('email');
        $nickname = $this->_post('nickname');
        $maxim = $this->_post('maxim');
        //$mobile = $this->_post('mobile');
        $old_password = $this->_post('old_password');
        $new_password = $this->_post('new_password');

        //邮件地址检验
        if(empty($email))
            return $this->sendStatus(false,'请正确填写您的邮箱地址','email', 10300);

        //昵称取值1-100字符
        if(strlen($nickname) < 1)
            return $this->sendStatus(false,'人在江湖飘,总得有名号','nickname', 10301);
        if(strlen($nickname) > 100)
            return $this->sendStatus(false,'昵称最长100个字符','nickname', 10302);
        
        //个性签名必须小于100字符
        if(strlen($maxim) > 100)
            return $this->sendStatus(false,'个性签名最长100个字符','maxim', 10303);

        //手机号验证
        //if(empty($mobile))
            //return $this->sendStatus(false,'您的手机号不正确','email', 10304);

        $model_user = MONK::getSingleton('Frame_Model_User');

        //检测email是否已经注册
        if($email != $this->user['user_email'] && $model_user->is_registered($email))
            return $this->sendStatus(false,'该邮箱已经被注册','email', 10305);
        
        //填充修改的数据
        $userdata = array();
        //修改邮箱
        if($email != $this->user['user_email']){
            $userdata['user_email'] = $email;
            $userdata['is_checked'] = 0;
        }
        //修改昵称
        if($nickname != $this->user['user_nickname'])
            $userdata['user_nickname'] = $nickname;
        //修改个性签名
        if($maxim != $this->user['user_maxim'])
            $userdata['user_maxim'] = $maxim;

        $_encrypt = MONK::getSingleton('Encrypt');
        $_encrypt->app_key = MONK::getConfig('app_key');
        //用户名密码检测
        if(empty($new_password) && empty($old_password)){
        }elseif(!empty($new_password) && !empty($old_password)){
            if($_encrypt->passwdEncode($old_password) == $this->user['user_password']){
                if(strlen($new_password) < 6)
                    return $this->sendStatus(false,'新密码需要至少有6位','new_password', 10306);
                else
                    $userdata['user_password'] = $_encrypt->passwdEncode($new_password);
            }else
                return $this->sendStatus(false,'现有密码输入错误','old_password', 10307);
        }else{
            if(empty($old_password))
                return $this->sendStatus(false,'请填写您的现有密码','old_password', 10308);
            if(empty($new_password))
                return $this->sendStatus(false,'请填写您的新密码','new_password', 10309);
        }
        //开始修改
        if(!empty($userdata)){
            $userdata['user_id'] = $this->user['user_id'];
            //更新远程信息
            $new_user = $model_user->update_setting($userdata);
            //更新用户本地信息
            $this->setAuth($_encrypt->authTokenEncode(
                $this->user['user_id'].' '.
                $email.' '.
                $nickname.' '.
                $maxim.' '.
                $_encrypt->passwdEncode(empty($new_password)?$this->user['user_password']:$new_password).' '.
                $this->user['user_avatar'].' '.
                '1**********'.' '.
                (isset($userdata['is_checked'])?'0':$this->user['is_checked'])
            ));
            return $this->sendStatus(true,'保存成功');
        }else{
            return $this->sendStatus(true,'保存成功');
        }
    }

}
